package cn.gxm.tools.migration.address;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class AddressMigration {
    private static final String driver = "com.mysql.jdbc.Driver";
    private static final Properties configs = new Properties();

    public static void main(String... args) throws SQLException, IOException {
        if (args.length == 0) {
            System.out.println("Usage: AddressMigration <config.properties>");
            System.exit(0);
        }

        configs.load(new FileInputStream(args[0]));

        // String url =  "jdbc:mysql://127.0.0.1:3306/gxmei?"
        // + "user=root&password=root&useUnicode=true&characterEncoding=UTF-8";
        String fromDbHost = getConfig("FROM_DB_HOST");
        String fromDbName = getConfig("FROM_DB_NAME");
        String fromDbUser = getConfig("FROM_DB_USER");
        String fromDbPassword = getConfig("FROM_DB_PASSWORD");

        String fromTableName = getConfig("FROM_TABLE");
        String fromIdName = getConfig("FROM_ID_NAME");
        String fromAddressName = getConfig("FROM_ADDRESS_NAME");


        String toDbHost = getConfig("TO_DB_HOST");
        String toDbName = getConfig("TO_DB_NAME");
        String toDbUser = getConfig("TO_DB_USER");
        String toDbPassword = getConfig("TO_DB_PASSWORD");

        String toTableName = getConfig("TO_TABLE");
        String toIdName = getConfig("TO_ID_NAME");
        String toAddress1Name = getConfig("TO_ADDRESS1_NAME");
        String toAddress2Name = getConfig("TO_ADDRESS2_NAME");


        DataLoader loader = new DataLoader(fromTableName, fromIdName, fromAddressName);
        Connection loaderConn = getConn(fromDbHost, fromDbName, fromDbUser, fromDbPassword);
        loader.setConn(loaderConn);

        DataSaver saver = new DataSaver(toTableName, toIdName, toAddress1Name, toAddress2Name);
        Connection saverConn = getConn(toDbHost, toDbName, toDbUser, toDbPassword);
        saver.setConn(saverConn);

        int count = loader.load((id, addr1, addr2) -> {
            try {
                saver.save(id, addr1, addr2);
            } catch (SQLException e) {
                System.err.printf("数据更新异常： %d, %s, %s", id, addr1, addr2);
                e.printStackTrace();
            }
        });

        saver.finish();
        loader.finish();

        System.out.printf("数据处理条数： %d", count);
    }

    /**
     * 获取数据库链接。
     */
    private static Connection getConn(String host, String db, String user, String passwd) {
        String url = "jdbc:mysql://" + host + ":3306/" + db + "?&useUnicode=true&characterEncoding=UTF-8";
        Connection conn = null;
        try {
            Class.forName(driver); //classLoader,加载对应驱动
            conn = (Connection) DriverManager.getConnection(url, user, passwd);
        } catch (ClassNotFoundException | SQLException e) {
            throw new RuntimeException(e);
        }

        return conn;
    }

    private static String getConfig(String key) {
        String value = configs.getProperty(key);
        if (value == null || value.isEmpty()) {
            System.out.printf("获取不到配置项：%s", key);
            System.exit(1);
        }

        return value;
    }
}
